#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int N = 3e3 + 10;
typedef pair<int, int> PII;
vector<PII> edges[N];
int T, n, m;
int cnt[N];
bool st[N];
int dist[N];
bool spfa()
{
	//因为多组数据，所以上来先初始化
	memset(dist, 0x3f, sizeof dist);
	//注意spfa算法初始化给的是无穷大，如果初始化是0的话不存在一个数小于0那就不会更新dist了
	memset(st, 0, sizeof st);
	memset(cnt, 0, sizeof cnt);

	queue<int> q;
	q.push(1);
	dist[1] = 0; 
	st[1] = true;
	
	while (q.size())
	{
		//跑一边spfa
		int x = q.front();q.pop();
		st[x] = false;
		for (auto& t : edges[x])
		{
			int v = t.first;int w = t.second;
			if (dist[x] + w < dist[v])
			{
				cnt[v] = cnt[x] + 1;
				dist[v] = dist[x] + w;
				if (cnt[v] >= n) return true;//因为最多n-1条边
				if (!st[v])
				{
					q.push(v);
					st[v] = true;
				}
			}
		}
	}
	return false;
}
int main()
{
	cin >> T;
	while (T--)
	{
		cin >> n >> m;
		//清空edges数组，多组数据，千万别忘了！！！
		for (int i = 1;i <= n;i++) edges[i].clear();
		for (int i = 1;i <= m;i++)
		{
			int u = 0, v = 0, w = 0;
			cin >> u >> v >> w;
			edges[u].push_back({ v,w });
			if (w >= 0)
			{
				edges[v].push_back({ u,w });
			}
		}
		int ret = spfa();
		if (ret) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}